import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:flutter_swiper/flutter_swiper.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      routes: {
        'home' : (context)=>MyHome()
      },
      initialRoute: 'home',
      onUnknownRoute: (RouteSettings settings)=>MaterialPageRoute(builder: (context)=>MyWrap()),
      onGenerateRoute: (RouteSettings settings){
        if(settings.name == '/') {
          return MaterialPageRoute(builder: (context) => MyHome());
        }
        Uri uri = Uri.parse(settings.name);
        uri.pathSegments[1];
      },
      title: "测试",
      home: MyHome(),
      debugShowCheckedModeBanner: false,
    );
  }
}

class MyHome extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          "头部",
          textDirection: TextDirection.ltr,
        ),
        leading: Icon(Icons.menu),
        actions: [Icon(Icons.settings)],
        centerTitle: true,
        elevation: 0,
      ),
      body: Jian(),
    );
  }
}

class MyRichText extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Text(
          "建哥",
          textDirection: TextDirection.ltr,
          style: TextStyle(
            fontSize: 30,
            color: Colors.red,
            fontWeight: FontWeight.w200,
            fontStyle: FontStyle.italic,
            decoration: TextDecoration.lineThrough,
            decorationStyle: TextDecorationStyle.dashed,
          ),
          textAlign: TextAlign.left,
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
        ),
        RichText(
          text: TextSpan(
              text: "Hello",
              style: TextStyle(backgroundColor: Colors.blue[600], fontSize: 16),
              children: [
                TextSpan(text: "jian", style: TextStyle(color: Colors.black12))
              ]),
        ),
      ],
    );
  }
}

class A extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Text(
          "SDA",
          style:
              TextStyle(fontSize: 10, color: Color.fromARGB(288, 255, 255, 0)),
        ),
        Container(
          child: Text("jainge"),
          width: 200,
          // height: 200,
          height: double.infinity,
          padding: EdgeInsets.all(10),
          margin: EdgeInsets.fromLTRB(0, 0, 10, 0),
          decoration: BoxDecoration(
              border: Border(
                  top: BorderSide(
                      color: Colors.blue[100], style: BorderStyle.solid)),
              borderRadius: BorderRadius.all(Radius.circular(30)),
              color: Colors.green[300],
              //设置渐变后会使背景色失效
              gradient: LinearGradient(
                  colors: [Colors.lightBlue[300], Colors.blue[50]])),
          alignment: Alignment.center,
          transform: Matrix4.translationValues(100, 0, 0),
        )
      ],
    );
  }
}

class MyContainer extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      height: 200,
      decoration: BoxDecoration(
        color: Colors.green,
        border: Border(top: BorderSide(width: 10, color: Colors.blue)),
        borderRadius: BorderRadius.circular(20),
      ),
      alignment: Alignment.center,
      transform: Matrix4.translationValues(0, 10, 0),
    );
  }
}

class MyColumn extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Row(
            children: [],
          )
        ],
      ),
    );
  }
}

class MyFlex extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: [
          Row(
            children: [
              Container(
                color: Colors.blue,
                height: 50,
              ),
              Expanded(
                child: Container(
                  color: Colors.blue,
                  height: 50,
                ),
                flex: 1,
              ),
              Flex(
                direction: Axis.horizontal,
                children: [],
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                verticalDirection: VerticalDirection.down,
                textDirection: TextDirection.rtl,
              ),
            ],
          )
        ],
      ),
    );
  }
}

class MyWrap extends StatelessWidget {
  List<String> _list = ["曹操", "曹仁", "曹丕", "许褚", "荀彧"];
  List<Widget> _weiGuo() {
    return _list
        .map((e) => Chip(
              label: Text(e),
              avatar: CircleAvatar(
                backgroundColor: Colors.blue,
                child: Text("魏"),
              ),
            ))
        .toList();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Wrap(
          spacing: 8,
          runSpacing: 8,
          alignment: WrapAlignment.center,
          runAlignment: WrapAlignment.center,
          children: [
            Chip(
              label: Text("刘备"),
              avatar: CircleAvatar(
                backgroundColor: Colors.blue,
                child: Text("蜀"),
              ),
            )
          ],
        )
      ],
    );
  }
}

class MyStack extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Stack(
        //声明未定位的子组件的排序方式
        textDirection: TextDirection.rtl,
        //声明未定位的子组件的对齐方式
        alignment: AlignmentDirectional.center,
        children: [
          CircleAvatar(
            backgroundImage: NetworkImage(
              "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg9.51tietu.net%2Fpic%2F2019-090920%2Fccuzyx3iq2jccuzyx3iq2j.jpg&refer=http%3A%2F%2Fimg9.51tietu.net&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1643621406&t=6c596321823b1917e74a2813b57de2ff",
            ),
            child: Text("jian"),
            radius: 200,
          ),
          Positioned(
            child: Text("热卖"),
            top: 100,
            right: 60,
          )
        ],
      ),
    );
  }
}

class MyCard extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Card(
          child: Column(
            children: [
              ListTile(
                leading: Icon(Icons.account_box_outlined),
                title: Text("张三"),
                subtitle: Text("董事长"),
              ),
              ListTile(
                title: Text("电话: 13556434645"),
              ),
              ListTile(title: Text("地址：大撒大撒大苏打"))
            ],
          ),
          margin: EdgeInsets.all(30),
          color: Colors.blue[300],
          shadowColor: Colors.black,
          elevation: 20,
          shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(40),
              side: BorderSide(width: 10, color: Colors.grey)),
        )
      ],
    );
  }
}

class MyButton extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(10),
      child: Wrap(
        children: [
          TextButton(
            onPressed: () {
              print("Textbutton");
            },
            child: Text("TextButton"),
          ),
          ElevatedButton(
            onPressed: () {
              print("ElevatedButton");
            },
            child: Text("ElevatedButton"),
          ),
          OutlinedButton(
            onPressed: () {
              print("OutlinedButton");
            },
            child: Text("OutlinedButton"),
            style: ButtonStyle(
                textStyle: MaterialStateProperty.all(TextStyle(fontSize: 16)),
                foregroundColor: MaterialStateProperty.resolveWith((states) {
                  if (states.contains(MaterialState.pressed)) {
                    //按下按钮时
                    return Colors.red;
                  }
                  return Colors.blue;
                }),
                backgroundColor: MaterialStateProperty.all(Colors.black),
                shadowColor: MaterialStateProperty.all(Colors.green),
                elevation: MaterialStateProperty.all(10),
                side: MaterialStateProperty.all(
                    BorderSide(color: Colors.blue[700], width: 4)),
                minimumSize: MaterialStateProperty.all(Size(100, 100)),
                overlayColor: MaterialStateProperty.all(Colors.green)),
          ),
          OutlinedButtonTheme(
            data: OutlinedButtonThemeData(
                style: ButtonStyle(
                    backgroundColor: MaterialStateProperty.resolveWith(
                        (states) => Colors.blue))),
            child: OutlinedButton(
              child: Text("测试"),
              onPressed: () {},
            ),
          ),
          IconButton(
            icon: Icon(Icons.ac_unit),
            onPressed: () {},
            color: Colors.blue,
            splashColor: Colors.lightBlue,
            tooltip: "怎么了",
          ),
          TextButton.icon(
            icon: Icon(Icons.ac_unit_rounded),
            label: Text("jian"),
            onPressed: () {},
          ),
          ButtonBar(
            children: [],
          ),
          BackButton(
            onPressed: () {},
          ),
          CloseButton()
        ],
      ),
    );
  }
}

class MyImage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        debugShowCheckedModeBanner: false,
        title: "Jian",
        home: Scaffold(
          appBar: AppBar(
            leading: Icon(Icons.ac_unit),
            title: Text("dsa"),
            centerTitle: true,
          ),
          body: Column(
            children: [
              Image.asset(
                "image/1.jpg",
                width: 100,
                fit: BoxFit.cover,
                repeat: ImageRepeat.repeat,
                colorBlendMode: BlendMode.darken,
              )
            ],
          ),
        ));
  }
}

class MySingleChildScrollView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      scrollDirection: Axis.horizontal,
      reverse: true,
      physics: BouncingScrollPhysics(), //  下拉回弹
      child: Row(
        children: [],
      ),
    );
  }
}

class MyListView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      physics: BouncingScrollPhysics(),
      child: Column(
        children: [
          ListView(
            physics: BouncingScrollPhysics(),
            scrollDirection: Axis.vertical,
            children: [
              ListTile(
                leading: Icon(Icons.sanitizer),
                title: Text("dsada"),
                trailing: Icon(Icons.dangerous),
                selected: true,
              )
            ],
          ),
        ],
      ),
    );
  }
}

class Jian extends StatelessWidget {
  final List<Widget> _users = List.generate(
      20,
      (index) => OutlinedButton(
            onPressed: () {},
            child: Text("姓名$index"),
          ));

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Column(
        children: [
          Container(
            height: 200,
            child: ListView(
              physics: BouncingScrollPhysics(),
              children: List.generate(
                  100,
                  (index) => ListTile(
                        leading: Icon(Icons.dangerous),
                        title: Text("$index"),
                      )),
            ),
          ),
          Container(
            height: 200,
            child: ListView.builder(
              itemCount: _users.length,
              itemExtent: 30,
              itemBuilder: (BuildContext context, int index) {
                return _users[index];
              },
            ),
          ),
          Container(
            height: 200,
            child: ListView.separated(
                itemBuilder: (BuildContext context, int index) {
                  return _users[index];
                },
                separatorBuilder: (BuildContext cotext, int index) {
                  return Divider(
                    thickness: 2,
                  );
                },
                itemCount: _users.length),
          )
        ],
      ),
    );
  }
}

class MyGridView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: [
          GridView(
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 3, //指定列数
                mainAxisSpacing: 20,
                crossAxisSpacing: 10,
                childAspectRatio: 1.5 //子组件的宽高比
                ),
            children: [
              Container(color: Colors.amber),
              Container(color: Colors.amber),
              Container(color: Colors.amber),
              Container(color: Colors.amber),
              Container(color: Colors.amber),
            ],
            physics: BouncingScrollPhysics(),
          ),
          GridView(
            gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
                maxCrossAxisExtent: 3,
                mainAxisSpacing: 10,
                crossAxisSpacing: 20,
                childAspectRatio: 1.5),
          ),
          GridView.count(
            crossAxisCount: 3,
            mainAxisSpacing: 20,
            crossAxisSpacing: 10,
          ),
          GridView.extent(
            maxCrossAxisExtent: 100,
          ),
          GridView.builder(
            gridDelegate:
                SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3),
            itemCount: 9,
            itemBuilder: (BuildContext context, int index) {
              return Container(
                color: Colors.accents[100],
              );
            },
          )
        ],
      ),
    );
  }
}

class MyCupertimo extends StatelessWidget {
  // if(Platform.isIOS) {
    
  // }
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: [
          AlertDialog(
            title: Text("提示"),
            content: Text("请选择你想要的风格"),
            actions: [
              TextButton.icon(
                label: Text("确认"),
                icon: Icon(Icons.check_box_outline_blank_outlined),
                onPressed: () {},
              ),
              OutlinedButton(onPressed: () {}, child: Text("取消"))
            ],
          ),
          CupertinoAlertDialog(
            title: Text(""),
            content: Text(""),
            actions: [
              CupertinoDialogAction(
                child: Text("确定"),
                onPressed: (){

                },
              )
            ],
          )
        ],
      ),
    );
  }
}

//safeArea可以避免屏幕的问题

class MyDio extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: ElevatedButton(
        child: Text("点击发送请求"),
        onPressed: (){

        },
      ),
    );
  }

  void getIpAddress() async {
    try{
      final url = "";
      Response response = await new Dio().get(url);
      print(response.data[""]);
    }catch(e){

    }
  }
}

class MySwiper extends StatelessWidget {
  List<String> content = [
    "dsads", "dasdsad", "dookdoa", "dads"
  ];
  @override
  Widget build(BuildContext context) {
    return Container(
      height: 200,
      child: Swiper(
        itemCount: content.length,
        itemHeight: 200,
        itemBuilder: (context, index) => Text(content[index]),
        pagination: SwiperPagination(), //轮播图的知识点
        control: SwiperControl(), //左右切换的按钮
        viewportFraction: 0.8,
        scale: 0.7,
        itemWidth: 300,
        layout: SwiperLayout.STACK,
      ),
    );
  }
}

class MyStatetuful extends StatefulWidget {
  @override
  _MyStatufulState createState() => _MyStatufulState();
}

class _MyStatufulState extends State<MyStatetuful> {
  int count = 0;
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        children: [
          Padding(padding: EdgeInsets.all(10),child: Text("$count"),),
          ElevatedButton(onPressed: (){
            setState(() {
              count++;
            });
          }, child: Text("点击加一"))
        ],
      ),
    );
  }
}

class MyDataTable extends StatefulWidget {
  @override
  _MyDataTableState createState() => _MyDataTableState();
}

class _MyDataTableState extends State<MyDataTable> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: SingleChildScrollView(
        child: DataTable(
          columns: [
            DataColumn(
              label: Text("姓名")
            ),
            DataColumn(
              label: Text("性别")
            ),
            DataColumn(
              label: Text("年龄"),
              onSort: (index, is){

              }
            ),
            DataColumn(
              label: Text("简介")
            ),
          ],
          rows: [
            DataRow(cells: [
              DataCell(
                Text("李子建")
              ),
              DataCell(
                Text("男")
              ),
              DataCell(
                Text("18")
              ),
              DataCell(
                Text("打算大苏打撒旦撒旦")
              ),
            ]),
            DataRow(cells: [
              DataCell(
                Text("李子建")
              ),
              DataCell(
                Text("男")
              ),
              DataCell(
                Text("18")
              ),
              DataCell(
                Text("打算大苏打撒旦撒旦")
              ),
            ]),
          ],
          sortColumnIndex: 0,
          sortAscending: true,
        ),
      ),
    );
  }
}

class MyInherit extends InheritedWidget {
  Widget child;
  int num;

  MyInherit({Key key, this.child, @required this.num}) : super(key: key, child: child);
  static MyInherit of(BuildContext context) {
    return context.dependOnInheritedWidgetOfExactType<MyInherit>();
  }

  @override
  bool updateShouldNotify(MyInherit oldWidget) {
    return true;
  }
}

class AKL extends StatefulWidget {
  @override
  _AKLState createState() => _AKLState();
}

class _AKLState extends State<AKL> {
  @override
  Widget build(BuildContext context) {
    return Container(
      //使用共享数据
      child: Text(MyInherit.of(context).num.toString()),
    );
  }
}

class ADK extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MyInherit(
      num: 1,
      child: AKL(),
    );
  }
}

class MyRoute1 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: [
          ElevatedButton(
        child: Text("匿名路由跳转"),
        onPressed: (){
          //pop是返回
          Navigator.push(
            context,
            MaterialPageRoute(builder: (context){
              //返回一个跳转的组件页面
            })
          );
        },
      ),
        ElevatedButton(onPressed: (){
          Navigator.pushNamed(context, 'home', arguments: {"name":"dsa"});
          // ModalRoute.of(context).settings.arguments.name;
        }, child: Text("命名路由跳转"))
        ],
      ),
    );
  }
}













